package com.jues.huichain.login.activity;

import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.TextUtils;
import android.webkit.WebView;
import android.widget.LinearLayout;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.jues.huichain.R;
import com.jues.huichain.R2;
import com.just.agentweb.AgentWeb;
import com.lm.sjy.titlebar.widget.CommonTitleBar;

import butterknife.BindView;
import health.lm.com.component_base.base.mvp.activity.XActivity;
import health.lm.com.component_base.webview.agent.AgentHelper;
import health.lm.com.component_base.webview.sonic.SonicImpl;
import health.lm.com.router.ARouterConstant;

/**
 * Created by Android Studio .
 * 作者：zhong
 * 日期：2019/2/28
 * 邮箱：15206394364@163.com
 * 介绍：
 * 修订：====================
 */
@Route(path = ARouterConstant.SimpleWebViewActivity)
public class SimpleWebViewActivity extends XActivity implements AgentHelper.ViewClient, AgentHelper.ChromeClient {
    @BindView(R2.id.title_bar)
    protected CommonTitleBar titlebar;
    @BindView(R2.id.linearLayout)
    protected LinearLayout linearLayout;
    protected AgentWeb mAgentWeb;
    protected SonicImpl mSonicImpl;
    @Autowired
    String url = "";
    Activity activity;

    protected void initWidget() {
        this.activity = this;
        AgentHelper.getInstance().setChromeClient(this);
        AgentHelper.getInstance().setViewClient(this);
        // 1. 首先创建SonicImpl
        mSonicImpl = new SonicImpl(url, this);
        // 2. 调用 onCreateSession
        mSonicImpl.onCreateSession();
        //3. 创建AgentWeb ,加入SonicWebViewClient中间件
        mAgentWeb = AgentHelper.getInstance().getAgentWeb(this, linearLayout, mSonicImpl, url);
        //4. 注入 JavaScriptInterface
        setJsInterface();
        //5. 最后绑定AgentWeb
        mSonicImpl.bindAgentWeb(mAgentWeb);
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_web;
    }

    @Override
    public Object newP() {
        return null;
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        initWidget();
    }

    /**
     * js 调用android 方法
     * 默认是没有的，需要使用的时候复写此方法
     */
    protected void setJsInterface() {
//        mAgentWeb.getJsInterfaceHolder().addJavaObject("",null);
    }

    @Override
    public void onProgressChanged(WebView view, int newProgress) {

    }

    @Override
    public void onReceivedTitle(WebView view, String title) {
        if (titlebar != null && !TextUtils.isEmpty(title)) {
            if (title.length() > 10) {
                title = title.substring(0, 10).concat("...");
            }
        }
        titlebar.getCenterTextView().setText(title);
    }

    @Override
    public void shouldOverrideUrlLoading(WebView webView, String url) {

    }

    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {

    }

    @Override
    public void onPageFinished(WebView view, String url) {

    }
}
